<?php

class Application_Model_Dao_Cathedral extends Zend_Db_Table_Abstract
{

	protected $_name = 'cathedrals';
	protected $_primary = 'id';

	public function init(){
			
	}
	
	public function getAllCathedral(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('cathedrals')->joinLeft('faculties', 'cathedrals.faculty_id = faculties.id', array('faculty_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	
public function getRowCathedral($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('cathedrals')->where('cathedrals.id = \''.$id.'\'')->joinLeft('faculties', 'cathedrals.faculty_id = faculties.id', array('faculty_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	
	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
		throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");
		return $result;
	}
	
	public function getCathedralByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
		throw new Exception("Katedra o ID $id nie istnieje.");
		return $row->toArray();
	}
	
	public function getCathedralByDesignation($designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
		throw new Exception("Katedra o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
	
	public function saveCathedral($cathedral) {
		$data = array(
			'faculty_id' => $cathedral->getFaculty(),
			'designation' => $cathedral->getDesignation(),
		);
		$result = $this->insert($data);
		return $result;
	}
	
	public function updateCathedral($cathedral) {
		$data = array(
			'faculty_id' => $cathedral->getFaculty(),
			'designation' => $cathedral->getDesignation(),
		);
		$result = $this->update($data, 'id = '. (int)$cathedral->getId());
		return $result;
	}
	
	public function deleteCathedral($cathedral){
		$result = $this->delete('id =' . (int)$cathedral->getId());
		return $result;
	}
	
	public function getToSelect() {		
		try {	
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = "SELECT c.id, c.designation as cathedral, f.short as faculty
				FROM cathedrals as c
				INNER JOIN faculties as f";
    		$stmt = $db->query($sql);
		} catch (Exception $exception) {
				Zend_Registry::get('logger')->log('Brak wyników. Exception: '.$exception, Zend_Log::NOTICE);
		}
		
    	$cathedrals[''] = "wybierz";
    	
    	while ($row = $stmt->fetch()) {
     		$cathedrals[ $row['id'] ] = $row['cathedral']." (".$row['faculty'].") "; 
     	}
     return $cathedrals;
	}
	
	public function getToSelectWhere($idFaculty) {
			try {	
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = "SELECT c.id, c.designation as cathedral, f.short as faculty
				FROM cathedrals as c
				INNER JOIN faculties as f
				ON c.faculty_id = ".$idFaculty;
    		$stmt = $db->query($sql);
		} catch (Exception $exception) {
				Zend_Registry::get('logger')->log('Brak wyników. Exception: '.$exception, Zend_Log::NOTICE);
		}
		$cathedrals[''] = "wybierz";
    	
    	while ($row = $stmt->fetch()) {
     		$cathedrals[ $row['id'] ] = $row['cathedral']." (".$row['faculty'].") "; 
     	}
     return $cathedrals;
	}
}
